<template>
	<view class="container">
		<uni-section class="mb-10" title="进库记录">
			<template v-slot:right>
				<button class="add-btn" @click="addReport" type="primary" size="mini">新增进库记录</button>
			</template>
		</uni-section>

		<!-- 搜索筛选区域 -->
		<view class="search-container">
			<view class="search-row">
				<view class="search-item  user-search">
					<text class="search-label">收运员:</text>
					<uni-easyinput v-model="searchData.userName" placeholder="请输入收运员" />
				</view>
			</view>
			<view class="search-row">
				<!-- <view class="search-item  user-search">
					<text class="search-label">商户油脂类型:</text>
					<ld-select :multiple="true" :list="options2" label-key="label" value-key="value" placeholder="请选择"
						clearable v-model="reportTypeList" @change="selectChange2"></ld-select>
				</view> -->
				<view class="search-item  user-search">
					<text class="search-label">油脂类型:</text>
					<ld-select :list="options2"
					                label-key="label" value-key="value"
					                placeholder="请选择"
					                clearable
					                v-model="searchData.type"
					                ></ld-select>
					<!-- <uni-data-select v-model="searchData.type" :localdata="options2" /> -->
				</view>
			</view>
			<view class="search-row">
				<uni-datetime-picker v-model="recordDate" type="datetimerange" rangeSeparator="至" />
			</view>
			<view class="search-btn-group">
				<button class="search-btn" @click="handleSearch">搜索</button>
				<button class="reset-btn" @click="handleReset">重置</button>
			</view>
		</view>
		<!-- 分页组件 -->
		<uni-pagination class="pagination" :current="pageNum" :total="total" :pageSize="pageSize"
			@change="handlePageChange" />

		<scroll-view class="scroll-container" scroll-y style="height: calc(100vh - 160rpx)">
			<view v-for="item in datas" :key="item.id" class="item-item">
				<view class="item-header">
					<text class="merchant-id">收运员: {{ item.userName }}</text>
					<view class="action-btns">
						<!--            <button class="edit-btn" @click="editRecord(item.id)">编辑</button>-->
						<button class="delete-btn" @click="delReport(item.id)">删除</button>
					</view>
				</view>
				<view class="item-content">
					<!--          <view class="field-row">-->
					<!--            <text class="label">操作人:</text>-->
					<!--            <text class="value">{{ item.userName }}</text>-->
					<!--          </view>-->
					<view class="field-row">
						<text class="label">进库日期:</text>
						<text class="value">{{ item.recordDate }}</text>
					</view>
					<view class="field-row">
						<text class="label">运输车辆车牌号:</text>
						<text class="value">{{selectDictLabel(options, item.plateNumber)}}</text>
					</view>
					<view class="field-row" v-if="item.reportType != null && item.reportType.includes('1')">
						<text class="label">灶台油总量:</text>
						<text class="value">{{ item.stoveQuantity }} 公斤</text>
					</view>
					<view class="field-row" v-if="item.reportType != null && item.reportType.includes('1')">
						<text class="label">灶台油含油率:</text>
						<text class="value">{{ item.stoveOilRate }} %</text>
					</view>
					<view class="field-row" v-if="item.reportType != null && item.reportType.includes('1')">
						<text class="label">灶台油纯油量:</text>
						<text class="value">{{ item.stovePureOil }} 公斤</text>
					</view>
					<view class="field-row" v-if="item.reportType != null && item.reportType.includes('1')">
						<text class="label">灶台油扣渣量:</text>
						<text class="value">{{ item.stoveResidue }} 公斤</text>
					</view>
					<view class="field-row" v-if="item.reportType != null && item.reportType.includes('2')">
						<text class="label">地上油总量:</text>
						<text class="value">{{ item.groundQuantity }} 公斤</text>
					</view>
					<view class="field-row" v-if="item.reportType != null && item.reportType.includes('2')">
						<text class="label">地上油含油率:</text>
						<text class="value">{{ item.groundOilRate }} %</text>
					</view>
					<view class="field-row" v-if="item.reportType != null && item.reportType.includes('2')">
						<text class="label">地上油纯油量:</text>
						<text class="value">{{ item.groundPureOil }} 公斤</text>
					</view>
					<view class="field-row" v-if="item.reportType != null && item.reportType.includes('2')">
						<text class="label">地上油扣渣量:</text>
						<text class="value">{{ item.groundResidue }} 公斤</text>
					</view>
					<view class="field-row" v-if="item.reportType != null && item.reportType.includes('3')">
						<text class="label">地下油总量:</text>
						<text class="value">{{ item.undergroundQuantity }} 公斤</text>
					</view>
					<view class="field-row" v-if="item.reportType != null && item.reportType.includes('3')">
						<text class="label">地下油含油率:</text>
						<text class="value">{{ item.undergroundOilRate }} %</text>
					</view>
					<view class="field-row" v-if="item.reportType != null && item.reportType.includes('3')">
						<text class="label">地下油纯油量:</text>
						<text class="value">{{ item.undergroundPureOil }} 公斤</text>
					</view>
					<view class="field-row" v-if="item.reportType != null && item.reportType.includes('3')">
						<text class="label">地下油扣渣量:</text>
						<text class="value">{{ item.undergroundResidue }} 公斤</text>
					</view>
				</view>
			</view>
		</scroll-view>
	</view>
</template>

<script>
	import {
		listReport,
		delReport
	} from '@/api/system/report.js';
import {
	getDicts
} from "@/api/system/dict/data";
	import {
		selectDictLabel  //主要是这个
	} from '@/utils/ruoyi'
	export default {
		data() {
			return {
				options: [],
				pageNum: 1,
				pageSize: 10,
				total: 0,
				datas: [],
				options2: [],
				searchData: {
					userName: '', // 收运员名称
					type: '',
					recordDateStart: new Date().toISOString().slice(0, 10) + " 00:00:00", // 开始日期
					recordDateEnd: new Date().toISOString().slice(0,10) + " 23:59:59" // 结束日期
				},
				recordDate: [new Date().toISOString().slice(0, 10) + " 00:00:00", new Date().toISOString().slice(0,
					10) + " 23:59:59"
				],
			};
		},
		onLoad(options) {
			// 加载数据
			// this.fetchData();
			// 数据字典
			getDicts("fqyz_type").then(response => {
				this.options2 = response.data.map(item => ({
					value: item.dictValue,
					label: item.dictLabel
				}))
			})
		},
		onShow(options) {
			this.fetchData();
		},
		methods: {
			selectDictLabel,
			// 加载数据
			async fetchData() {
				try {
					// 数据字典
					await getDicts("sys_car_number").then(response => {
						this.options = response.data.map(item => ({
							value: item.dictValue,
							text: item.dictLabel
						}))
					})
					const res = await listReport({
						'pageNum': this.pageNum,
						'pageSize': this.pageSize,
						...this.searchData
					});
					this.datas = res.rows;
					this.total = res.total;
				} catch (error) {
					uni.showToast({
						title: '加载失败',
						icon: 'none'
					});
				}
			},
			// 翻页
			handlePageChange(e) {
				this.pageNum = e.current;
				this.fetchData();
			},
			// 搜索
			handleSearch() {
				console.log(this.searchData)
				this.searchData.recordDateStart = this.recordDate[0]
				this.searchData.recordDateEnd = this.recordDate[1]
				// 重置页码
				this.pageNum = 1;
				this.fetchData();
				// this.fetchAmountData();
			},
			// 重置搜索条件
			handleReset() {
				this.searchData = {
					userName: '',
					type:'',
					recordDateStart: new Date().toISOString().slice(0, 10) + " 00:00:00",
					recordDateEnd: new Date().toISOString().slice(0, 10) + " 23:59:59"
				};
				// 重置页码并重新加载数据
				this.pageNum = 1;
				this.fetchData();
				// this.fetchAmountData();
			},
			// 删除
			async delReport(id) {
				uni.showModal({
					title: '确认删除',
					content: '是否删除该记录？',
					success: async (res) => {
						if (res.confirm) {
							try {
								await delReport(id);
								uni.showToast({
									title: '删除成功'
								});
								this.fetchData();
							} catch (error) {
								uni.showToast({
									title: '删除失败',
									icon: 'none'
								});
							}
						}
					}
				});
			},
			// 跳转新增页面
			addReport() {
				uni.navigateTo({
					url: `/pages/report/add`
				});
			},
		}
	}
</script>



<style scoped>
	.container {
		padding: 20rpx;
		background-color: #f5f5f5;
	}

	.toolbar {
		/* margin-bottom: 20rpx; */
	}

	.add-btn {}

	.pagination {
		margin: 20rpx 0;
	}

	.scroll-container {
		border-radius: 12rpx;
		background-color: white;
		overflow: hidden;
	}

	.item-item {
		margin: 10rpx 0;
		padding: 20rpx;
		background-color: white;
		border-radius: 12rpx;
		box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.1);
	}

	.item-header {
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-bottom: 20rpx;
		border-bottom: 1rpx solid #e5e5e5;
		padding-bottom: 10rpx;
	}

	.merchant-id {
		font-size: 32rpx;
		color: #333;
		font-weight: bold;
	}

	.action-btns {
		display: flex;
		gap: 10rpx;
	}

	.edit-btn {
		background-color: #55B3FF;
		color: white;
		border-radius: 8rpx;
		padding: 8rpx 16rpx;
		font-size: 24rpx;
	}

	.delete-btn {
		background-color: #FF6666;
		color: white;
		border-radius: 8rpx;
		padding: 8rpx 16rpx;
		font-size: 24rpx;
	}

	.field-row {
		display: flex;
		align-items: center;
		margin-bottom: 12rpx;
	}

	.label {
		width: 300rpx;
		color: #666;
		font-weight: bold;
	}

	.value {
		flex: 1;
		color: #333;
		white-space: nowrap;
		overflow: hidden;
		text-overflow: ellipsis;
	}

	/* 响应式布局 */
	@media (min-width: 750rpx) {
		.scroll-container {
			width: 70%;
			margin: 0 auto;
		}
	}

	.search-container {
		background-color: #fff;
		border-radius: 12rpx;
		padding: 20rpx;
		margin-bottom: 20rpx;
		box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
	}

	.search-row {
		display: flex;
		flex-wrap: wrap;
		gap: 20rpx;
		margin-bottom: 20rpx;
	}

	.search-item {
		display: flex;
		align-items: center;
		width: 100%;
	}

	.search-label {
		display: inline-block;
		width: 200rpx;
		margin-right: 10rpx;
		color: #666;
	}

	.search-btn-group {
		display: flex;
		justify-content: center;
		gap: 30rpx;
	}

	.search-btn,
	.reset-btn {
		width: 200rpx;
		height: 70rpx;
		line-height: 70rpx;
		border-radius: 10rpx;
		color: #fff;
		font-size: 28rpx;
	}

	.search-btn {
		background-color: #1989fa;
	}

	.reset-btn {
		background-color: #999;
	}

	.user-search {
		flex: 1;
	}
</style>
